package test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownLoader {
	static public void download(String newName, String targetPath, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/unknown");
		response.setHeader("Content-disposition", "attachment;filename=" + newName + ";");
		
		BufferedInputStream bis = null;
		OutputStream os = null;
		int read = 0;
		byte readByte[] = new byte[4096];
		try {
			bis = new BufferedInputStream(new FileInputStream(targetPath));
			os = response.getOutputStream();
			while ( (read = bis.read(readByte, 0, 4096)) != -1 ) {
				os.write(readByte, 0, read);
			}
			os.flush();
		} finally {
			if (os != null) os.close();
			if (bis != null) bis.close();
		}
	}
}
